package com.baiiu.filter.typeview;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.baiiu.filter.adapter.BaseBaseAdapter;
import com.baiiu.filter.interfaces.OnFilterItemClickListener;
import com.baiiu.filter.util.CommonUtil;
import com.baiiu.filter.util.UIUtil;

import java.util.List;

/**
 * author: baiiu
 * date: on 16/2/16 15:46
 * description:
 */
public class SingleGridView<DATA> extends GridView implements AdapterView.OnItemClickListener {
    private Context mContext;
    private BaseBaseAdapter<DATA> mAdapter;
    private OnFilterItemClickListener<DATA> mOnItemClickListener;
    private int maxHeight;
    //    int minHeight;
    private int gvContentHeight;

    public SingleGridView(Context context) {
        this(context, null);
    }

    public SingleGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SingleGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        setVerticalScrollBarEnabled(false);
        setHorizontalScrollBarEnabled(false);
        setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        setSelector(new ColorDrawable(Color.TRANSPARENT));
        setNumColumns(3);
        setBackgroundColor(Color.WHITE);
        setSmoothScrollbarEnabled(false);
//        minHeight = UIUtil.dp(context, 300);
        int dp = UIUtil.dp(context, 12);

        setVerticalSpacing(dp);
        setHorizontalSpacing(dp);
        setPadding(dp, dp, dp, dp);
        setOnItemClickListener(this);
    }

    public SingleGridView<DATA> adapter(BaseBaseAdapter<DATA> adapter) {
        this.mAdapter = adapter;
        setAdapter(adapter);
        return this;
    }

    public SingleGridView<DATA> onItemClick(OnFilterItemClickListener<DATA> onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
        return this;
    }


    public void setList(List<DATA> list, int checkedPositoin) {
        mAdapter.setList(list);

        if (checkedPositoin != -1) {
            setItemChecked(checkedPositoin, true);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (CommonUtil.isFastDoubleClick()) {
            return;
        }

        DATA item = mAdapter.getItem(position);

        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(item, position);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.EXACTLY;
        int heightMs;
        maxHeight = ((FrameLayout) getParent()).getMeasuredHeight() - UIUtil.dp(mContext, 50 + 90);//顶部筛选栏占用了50dp，需减去
        if (gvContentHeight == 0) {
            ListAdapter adapter = getAdapter();
            if (adapter == null) {
                return;
            }
            View v = adapter.getView(0, null, this);
            v.measure(0, 0);
            int itemHeight = v.getMeasuredHeight();

            int row = adapter.getCount() / getNumColumns();
            if (adapter.getCount() % getNumColumns() != 0) {
                ++row;
            }
            gvContentHeight = itemHeight * row + UIUtil.dp(mContext, 12) * (row - 1) + getPaddingTop() + getPaddingBottom();
        }
//        if (gvContentHeight < minHeight) {
//            setMeasuredDimension(width, minHeight);
//            heightMs = MeasureSpec.makeMeasureSpec(minHeight, heightMode);
//        } else
        if (gvContentHeight > maxHeight) {
            setMeasuredDimension(width, maxHeight);
            heightMs = MeasureSpec.makeMeasureSpec(maxHeight, heightMode);
        } else {
            setMeasuredDimension(width, gvContentHeight);
            heightMs = MeasureSpec.makeMeasureSpec(gvContentHeight, heightMode);
        }
        super.onMeasure(widthMeasureSpec, heightMs);
    }
}
